package com.markerhub.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.Blog;
import com.markerhub.service.BlogService;
import com.markerhub.util.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lisipeng
 */
@RestController
public class BlogController {

    @Autowired
    BlogService blogService;

    @GetMapping("/blogs")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage) {

        Page page = new Page(currentPage, 5);
        IPage pageData = blogService.page(page, new QueryWrapper<Blog>().orderByDesc("created"));

        return Result.succ(pageData);
    }

    @GetMapping("/blog/{id}")
    public Result detail(@PathVariable(name = "id") Long id) {
        Blog blog = blogService.getById(id);
        Assert.notNull(blog, "该博客已被删除");

        return Result.succ(blog);
    }

    @RequiresAuthentication
    @PostMapping("/blog/edit")
    public Result edit(@Validated @RequestBody Blog blog) {
//        Assert.isTrue(false, "公开版不能任意编辑！");

        Blog temp = null;
        if(blog.getId() != null) {
            temp = blogService.getById(blog.getId());
            // 只能编辑自己的文章
            System.out.println(ShiroUtil.getProfile().getId());
            Assert.isTrue(temp.getUserId().longValue() == ShiroUtil.getProfile().getId().longValue(), "没有权限编辑");
        } else {
            temp = new Blog();
            temp.setUserId(ShiroUtil.getProfile().getId());
            temp.setCreated(LocalDateTime.now());
            temp.setStatus(0);

        }

        BeanUtil.copyProperties(blog, temp, "id", "userId", "created", "status");
        blogService.saveOrUpdate(temp);

        return Result.succ(null);
    }

    /**
     * 发表文章
     * */
    @RequestMapping(path="/blog/editblog",method = RequestMethod.POST)
    public Result editBlog(@Validated @RequestBody Blog blog) {
        Blog temp = new Blog();
        temp.setTitle(blog.getTitle());
        temp.setContent(blog.getContent());
        temp.setStatus(blog.getStatus());
        if (!blogService.save(temp)) return Result.fail("发表文章失败");
        return  Result.succ(temp);
    }

    /**
     * 根据文章id删除文章
     *
     * @param id 文章id
     * */
    @PostMapping("/blog/delete/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        Boolean b = blogService.removeById(id);
        return Result.succ(b);
    }

    /**
     * 文章雷达统计信息
     * */
    @PostMapping("/bloginfo")
    public Result bloginfo() {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(9);
        list.add(7);
        list.add(7);
        list.add(4);
        list.add(8);
        return Result.succ(list);
    }

    /**
     * 测试 仿扫码登录,判断是否扫码
     * */
    @PostMapping("/login/imgloginpd/{id}")
    public Result imgloginpost(@PathVariable("id") Integer id) {
        Blog b = blogService.getById(id);
        return Result.succ(b);
    }

    /**
     * 测试 仿扫码登录,此时扫码,改变状态
     * */
    @GetMapping("/login/imgloginchange/{id}/{status}")
    public Result imgloginget(@PathVariable("id") Long id ,@PathVariable("status") Integer status) {
        Blog blog = new Blog();
        blog.setStatus(status);
        blog.setId( id);
        Boolean b = blogService.updateById(blog);
        return Result.succ(b);
    }

}
